<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
	<div style="height: 400px; overflow-y: scroll;">
		<table border="1" cellspacing="0" style="width: 100%;border-color: #fff;border-collapse: collapse;">
		  <thead>
		    <tr>
		      <th style="height: 40px;background-color: #ddd;" colspan="3">服务项目</th>
		      <th style="height: 40px;background-color: #fafafa;" colspan="1">价格</th>
		    </tr>
		  </thead>
		  <tbody>
		    <template v-for="tb in formData.categoryList" :key="tb.id">
			  <template v-if="tb.goodsList">
				  <template v-for="(tb2, t2) in tb.goodsList" :key="tb2.id">
					  <tr>
						  <td v-if="t2 == 0" align="center" :rowspan="tb.goodsList.length">{{tb.name}}</td>
						  <td align="center" colspan="2">{{tb2.name}}</td>
						  <td align="center" style="width: 80px;padding: 0px;">
							  <el-switch
							    v-model="tb2.deleted"
							    :active-value="true"
							    :inactive-value="false"
							  />
						  </td>
					  </tr>
				  </template>
			  </template>
			  <template v-if="tb.categoryList">
				  <template v-for="(tb2, t2) in tb.categoryList" :key="tb2.id">
					  <template v-for="(tb3, t3) in tb2.goodsList" :key="tb3.id">
						  <tr>
							  <td v-if="t3==0 && t2 == 0" align="center" :rowspan="tb.rowspan">{{tb.name}}</td>
							  <td v-if="t3 == 0" align="center" :rowspan="tb2.goodsList.length">{{tb2.name}}</td>
							  <td align="center">{{tb3.name}}</td>
							  <td align="center" style="width: 80px;padding: 0px;">
								  <el-switch
								    v-model="tb3.deleted"
								    :active-value="true"
								    :inactive-value="false"
								  />
							  </td>
						  </tr>
					  </template>
				  </template>
			  </template>
		    </template>
		  </tbody>
		</table>
	</div>
    
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">保 存</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ClerkApplyApi, ClerkApplySaveReqVO } from '@/api/worker/clerkapply'

/** 店员等级 表单 */
defineOptions({ name: 'ClerkApplyGoodsForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
	id: undefined, // 编号
	categoryList: [] // 价格
})

const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = "店员技能管理设置"
  formType.value = type
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
	  formData.value.id = id
      formData.value.categoryList = await ClerkApplyApi.getWorkerGoodsList(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 提交请求
  formLoading.value = true
  try {
	const data = formData.value as unknown as ClerkApplySaveReqVO
    await ClerkApplyApi.saveWorkerGoodsList(data)
    message.success(t('common.updateSuccess'))
	
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

const headerCellStyle = ({ row, column, rowIndex, columnIndex }) => {
  // 第一步：设置表头的第0列暂不操作，将地1列和第2列隐去使其消失
  if ((columnIndex == 1) | (columnIndex == 2)) {
	return { display: "none" };
  }
  // 第二步， 由于1、2列没有了，后续列就会贴上来（后续列往左错位问题）
  if ((rowIndex == 0) & (columnIndex == 0)) {
	// 解决后续列错位问题，就是将隐去的第1、2列的位置再补上，通过第0列来补
	nextTick(() => {
	  document.querySelector(`.${column.id}`).setAttribute("colspan", "3");
	});
	
  }
}

</script>